﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Windows.Data;
using SMS.Entities.BaseClasses;
using SMS.Model;
using System.Collections.ObjectModel;
using SMS.Entities.Admin;
using System.Collections.Generic;
using SMS.Common.Silverlight;
using System.ComponentModel;

namespace SMS.ViewModel.Admin
{
    public class ViewModel_UpdateUser:BaseNotify
    {
        DataGrid LoadDataGrid;
        AuthenticationModel _model;
        public ViewModel_UpdateUser()
        {
            //InitializeCommand();
            //InitializeModel();
            SearchCommand = new DelegateCommand(Search_Click, null);
            SaveCommand = new DelegateCommand(Save_Click, null);
            NewCommand = new DelegateCommand(New_Click, null);
            if (!DesignerProperties.IsInDesignTool)
            {
                _model = new Model.AuthenticationModel();
                //_model.OnServiceError += new Action<Entities.SMSException>(_model_OnServiceError);
                _model.SaveNewUserCompleted += new Action<ApplicationUser>(_model_SaveNewUserCompleted);
                _model.GetApplicationUserListCompleted += new Action<ObservableCollection<ApplicationUser>>(_model_GetApplicationUserListCompleted);
            }
        }

        void _model_GetApplicationUserListCompleted(ObservableCollection<ApplicationUser> obj)
        {

            UserMessage = obj.Count + " User found";
            UsersList = new List<Entities.Admin.ApplicationUser>(obj);
            PagedUsersList = new PagedCollectionView(UsersList);
        }

        void _model_SaveNewUserCompleted(ApplicationUser obj)
        {
            UserMessage = "User saved successfully.";
            SelectedMaster.Login_ID = obj.Login_ID;
        }

        #region Commented code...

        //#region "Methods"

        //private void InitializeModel()
        //{
        //    AbcApplicationUserList = new ObservableCollection<ApplicationUser>();
        //    authModelObject = new AuthenticationModel();
        //    authModelObject.GetApplicationUserListCompleted += new Action<ObservableCollection<ApplicationUser>>(authModelObject_GetApplicationUserListCompleted);
        //    authModelObject.GetApplicationUserList(App.LoggedinUserName, "N");
        //}

        //void authModelObject_GetApplicationUserListCompleted(ObservableCollection<ApplicationUser> obj)
        //{
        //    if (obj != null)
        //    {                
        //        ApplicationUserList = obj as ObservableCollection< SMS.Entities.Admin.ApplicationUser>;
        //        AbcApplicationUserList = ApplicationUserList;
        //        ApplicationUserObjecct = obj.FirstOrDefault();
        //        PageCollectionView = new PagedCollectionView(ApplicationUserList);
                
        //    }
        //}

        //private void InitializeCommand()
        //{
        //    SearchCommand = new SMS.Common.Silverlight.DelegateCommand(Search_Click, null);
        //    GridLoadCommand = new SMS.Common.Silverlight.DelegateCommand(GridLoaded, null);
        //    UpdateCommand = new SMS.Common.Silverlight.DelegateCommand(Update, null);
        //    RemoveCommand = new SMS.Common.Silverlight.DelegateCommand(Remove, null);
        //}

        //public void GridLoaded(object sender)
        //{
        //    LoadDataGrid = sender as DataGrid;
        //    if(LoadDataGrid!=null)
        //        LoadDataGrid.LayoutUpdated += new EventHandler(LoadDataGrid_LayoutUpdated);
        //}

        //void LoadDataGrid_LayoutUpdated(object sender, EventArgs e)
        //{
        //    List<HyperlinkButton> TaskButtonCollection = ControlFinder.GetChildren<HyperlinkButton>(LoadDataGrid);
        //    foreach (HyperlinkButton btn in TaskButtonCollection)
        //    {
        //        if (btn.Name == "hyp_UpdateUser")
        //            btn.Command = UpdateCommand;
        //        if (btn.Name == "hyp_DeleteUser")
        //        {
        //            btn.Command = RemoveCommand;
        //        }
        //    }
        //}

        //public void Search_Click(object sender)
        //{
        //    authModelObject.GetApplicationUserListCompleted+=new Action<ObservableCollection<ApplicationUser>>( GetApplicationUserListCompleted);
        //    authModelObject.GetApplicationUserList(App.LoggedinUserName, SearchText);
        //}

        //void GetApplicationUserListCompleted(ObservableCollection<ApplicationUser> obj)
        //{
        //    if (obj != null)
        //    {
        //        ApplicationUserList = obj as ObservableCollection<SMS.Entities.Admin.ApplicationUser>;
        //        PageCollectionView = new PagedCollectionView(ApplicationUserList);

        //    }
        //}

        //public void Update(object param)
        //{
        //    var show = new SMS.Views.NewUserChildWindow("Edit", ApplicationUserObjecct);
        //    show.Closed += (s, e) =>
        //    { };
        //    show.Show();
        //}

        //public void Remove(object param)
        //{
 
        //}

        //#endregion

        //#region "Property"

        //private PagedCollectionView _pageCollectionView;

        //public PagedCollectionView PageCollectionView
        //{
        //    get { return _pageCollectionView; }
        //    set { _pageCollectionView = value; OnPropertyChanged("PageCollectionView"); }
        //}
        

        //private ObservableCollection<ApplicationUser> _applicationUserList;
        //public ObservableCollection<ApplicationUser> ApplicationUserList
        //{
        //    get { return _applicationUserList; }
        //    set { _applicationUserList = value; OnPropertyChanged("ApplicationUserList"); }
        //}

        //private ObservableCollection<ApplicationUser> _acbApplicationUserList;
        //public ObservableCollection<ApplicationUser> AbcApplicationUserList
        //{
        //    get { return _acbApplicationUserList; }
        //    set { _acbApplicationUserList = value; OnPropertyChanged("AbcApplicationUserList"); }
        //}

        //private ApplicationUser _applicationUserObject;

        //public ApplicationUser ApplicationUserObjecct
        //{
        //    get { return _applicationUserObject; }
        //    set { _applicationUserObject = value; OnPropertyChanged("ApplicationUserObjecct"); }
        //}

        //private string  _SearchText;

        //public string  SearchText
        //{
        //    get { return _SearchText; }
        //    set { _SearchText = value; OnPropertyChanged("SearchText"); }
        //}
        

        //#endregion
        //#region "Delegate Command Property"

        //public ICommand SearchCommand { get; set; }
        //public ICommand GridLoadCommand { get; set; }
        //public ICommand UpdateCommand { get; set; }
        //public ICommand RemoveCommand { get; set; }

        //#endregion
        #endregion
        void _model_OnSearchCompleted(System.Collections.ObjectModel.ObservableCollection<Entities.Category> obj)
        {
        }

        void _model_OnSaveCategoryCompleted(int obj)
        {
            
        }

        void _model_OnServiceError(Entities.SMSException obj)
        {
            ErrorMessage = obj.Message;
        }



        private List<Entities.Admin.ApplicationUser> _UsersList;

        public List<Entities.Admin.ApplicationUser> UsersList
        {
            get { return _UsersList; }
            set { _UsersList = value; OnPropertyChanged("UsersList"); }
        }

        //private Category _SelectedCategory;

        //public Category SelectedCategory
        //{
        //    get { return _SelectedCategory; }
        //    set { _SelectedCategory = value; OnPropertyChanged("SelectedCategory"); }
        //}

        private Entities.Admin.ApplicationUser _SelectedMaster;

        public Entities.Admin.ApplicationUser SelectedMaster
        {
            get { return _SelectedMaster; }
            set { _SelectedMaster = value; OnPropertyChanged("SelectedMaster"); }
        }

        private PagedCollectionView _PagedUsersList;

        public PagedCollectionView PagedUsersList
        {
            get { return _PagedUsersList; }
            set { _PagedUsersList = value; OnPropertyChanged("PagedUsersList"); }
        }

        private string _SearchText;

        public string SearchText
        {
            get { return _SearchText; }
            set { _SearchText = value; OnPropertyChanged("SearchText"); }
        }

        private string _Text;

        public string Text
        {
            get { return _Text; }
            set { _Text = value; OnPropertyChanged("Text"); }
        }


        public ICommand SearchCommand { get; private set; }

        public void Search_Click(object param)
        {
            _model.GetApplicationUserList(App.LoggedinUserName,SearchText);

            UserMessage = "Searching users...";
        }

        public ICommand SaveCommand { get; set; }

        public void Save_Click(object param)
        {
            if (SelectedMaster != null)
            {

                _model.SaveNewUser(App.LoggedinUserName ,SelectedMaster);

                UserMessage = "Saving user...";
            }
            else
            {
                ErrorMessage = "No user to save";
            }
        }

        public ICommand NewCommand { get; set; }

        public void New_Click(object param)
        {
            if (UsersList == null)
                UsersList = new List<Entities.Admin.ApplicationUser>();
            var k = new Entities.Admin.ApplicationUser() { IsLive = true };
            UsersList.Add(k);
            PagedUsersList = new PagedCollectionView(UsersList);
            PagedUsersList.MoveToLastPage();
            SelectedMaster = k;
        }


    }
}
